package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: cloud2020
 * @description: TODO
 * @author: YuWenYi
 * @create: 2021-04-15 09:32
 **/
@RestController
//@DefaultProperties(defaultFallback = "paymentInfo_Global_FallbackMethod")
//使用这个注解后,如果服务方法上的 @HystrixCommand注解没有配置fallbackMethod属性,那么就会找这个默认的全局fallback方法!
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_OK(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
/*    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })*/
    //客户端表示只愿意等待1.5秒,如果超过1,5秒就会报错,使用服务降级.
    //此时哪怕服务器的限制是3秒,实际调用是2秒没有问题,此时也会因为客户端超时而产生问题
    //@HystrixCommand    //开启服务降级功能
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return paymentHystrixService.paymentInfo_TimeOut(id);
    }
    //调用出错后的服务降级兜底方法!
    public String paymentInfo_TimeOutHandler(Integer id){
        return "本80系统繁忙，请稍后再试,id："+id+"\t"+"。。。";
    }

    //下面是全局的fallback方法!
    public String paymentInfo_Global_FallbackMethod(){
        return "客户端80--->Global异常处理信息，请稍后再试，。。。";
    }

}
